package dao.Impl;

//import dao.CompileFile;
//import dao.CreateFile;


import dao.CompileFile;
import dao.CreateFile;

import java.io.*;

public class CompileJavaFile  implements CompileFile {

    private String fileName;

    public CompileJavaFile(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public String compile() {
        String result="";

        File file=null;
        try {
            Process proc=Runtime.getRuntime().exec("cmd.exe /c javac  G:\\compile\\"+fileName+".java");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String line;
            boolean b;
//            System.out.println("sdfsdf:sadds:第三方："+stdError.readLine());
            proc.waitFor();
            if(stdError.readLine()==null){
                return "true";
            }else{
                while((line= stdError.readLine())!=null){

                    result+=line;
                    result+="\n";
                }
            }

            if(result=="\n"){
                System.out.println("dsfsdfd");
            }
            System.out.println(result);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String run()  {
        String[] cmd={"cmd.exe /c javac "};
        String str="cmd.exe /c set CLASSPATH=G:\\compile && java "+fileName;
        String arr[] = {"CLASSPATH=G:\\compile","PATH=D:\\jdk1.8.0_241\\bin"};
        String line=null;
        String result="";

        try {
            Runtime rt=Runtime.getRuntime();
            Process proc=rt.exec("cmd /c java "+fileName,arr,new File("G:\\compile"));
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

            while((line= stdInput.readLine())!=null){
                result+=line;
                result+="\n";

            }
//            while ((line= stdError.readLine())!=null){
//                result+=line;
//                result+="\n";
//            }
            System.out.println(result);
            int exitVal= proc.waitFor();
            System.out.println("Process exitValue "+exitVal);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String s="   class Tet{\n" +
                "        public static void main(String[] args) {\n" +
                "\n" +
                "            for(int i=1;i<10i++)\n" +
                "                System.out.println(\"sdfsf\");\n" +
                "        }\n" +
                "    } "

    ;
        CreateFile createFile=new CreateFileImpl(s,"java");
        createFile.setJavaFileName();
        createFile.writeFile();


        CompileFile compileFile=new CompileJavaFile(createFile.getFileName());
        System.out.println(compileFile.compile());
        try {
            System.out.println(compileFile.run());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
